package server.quest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import provider.PureData;
import provider.PureDataEntity;
import provider.WzXML.PureDataType;

public class PureCustomQuestData implements PureData {

    private List<PureCustomQuestData> children = new LinkedList<PureCustomQuestData>();
    private String name;
    private Object data;
    private PureDataEntity parent;

    public PureCustomQuestData(String name, Object data, PureDataEntity parent) {
	this.name = name;
	this.data = data;
	this.parent = parent;
    }

    public void addChild(PureData child) {
	children.add((PureCustomQuestData) child);
    }

    public String getName() {
	return name;
    }

    public PureDataType getType() {
	return PureDataType.UNKNOWN_TYPE;
    }

    public List<PureData> getChildren() {
	PureData[] ret = new PureData[children.size()];
	ret = children.toArray(ret);
	return new ArrayList<PureData>(Arrays.asList(ret));
    }

    public PureData getChildByPath(String name) {
	if (name.equals(this.name)) {
	    return this;
	}
	String lookup, nextName;
	if (name.indexOf("/") == -1) {
	    lookup = name;
	    nextName = name;
	} else {
	    lookup = name.substring(0, name.indexOf("/"));
	    nextName = name.substring(name.indexOf("/") + 1);
	}
	for (PureData child : children) {
	    if (child.getName().equals(lookup)) {
		return child.getChildByPath(nextName);
	    }
	}
	return null;
    }

    public Object getData() {
	return data;
    }

    public Iterator<PureData> iterator() {
	return getChildren().iterator();
    }

    public PureDataEntity getParent() {
	return parent;
    }
}
